//------------------------------------------------------------------------------
#include <QtGui>
#include "CTextRevWidget.h"
#include "CMarkDown.h"
//------------------------------------------------------------------------------
CTextRevWidget::CTextRevWidget(QWidget *parent) : CTextW(parent) {
	setupUi(this);
	setTextEdit(text);
	cmdSetToRev->setEnabled(false);
}
//------------------------------------------------------------------------------
void CTextRevWidget::setValue(QString value) {
	CTextW::setValue(value);
	updateHtml();
}
//------------------------------------------------------------------------------
void CTextRevWidget::clearRevision(void) { 
	manageRevision=true;
	cbRevision->clear();
	cmdSetToRev->setEnabled(false);
	manageRevision=false;
}
//------------------------------------------------------------------------------
void CTextRevWidget::addRevision(QString revision) {
	manageRevision=true;
	cbRevision->addItem(revision);
	cmdSetToRev->setEnabled(false);
	manageRevision=false;
}
//------------------------------------------------------------------------------
void CTextRevWidget::setToLastRevision(void) { 
	manageRevision=true;
	cbRevision->setCurrentIndex(cbRevision->count()-1);
	cmdSetToRev->setEnabled(false);
	manageRevision=false;
}
//------------------------------------------------------------------------------
void CTextRevWidget::updateHtml(void) {
	QString html=CMarkDown::toHtml(CTextW::getValue());
	webView->setHtml(html);
	webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
}
//------------------------------------------------------------------------------
void CTextRevWidget::on_text_textChanged(void) {
	updateHtml();
	if(!manageRevision) {
		emit(textChanged());
	}
}
//------------------------------------------------------------------------------
void CTextRevWidget::on_cbRevision_currentIndexChanged(int) {
	if(!manageRevision)	{
		manageRevision=true;
		cmdSetToRev->setEnabled(cbRevision->currentIndex() < cbRevision->count()-1);
		emit(loadRevision(cbRevision->currentText()));
		manageRevision=false;
	}
}
//------------------------------------------------------------------------------
void CTextRevWidget::on_cmdSetToRev_clicked(bool) {
	emit(textChanged());
}
//------------------------------------------------------------------------------
void CTextRevWidget::on_webView_linkClicked(const QUrl & url) {
	QDesktopServices::openUrl(url);
}
//------------------------------------------------------------------------------
